using Debugger2 = Debugger;
using UnityEngine;

public class WhiteBall : PoolBall
{
	public static bool CueBallSiding;

	private Vector3 screenPoint;

	private Vector3 offset;

	private Constraint m_constraint;

	private PoolBall m_targetBall;

	private Vector3 m_targetPos;

	public LayerMask layermask;

	public bool Foul = true;

	[SerializeField]
	private Vector3 m_TestVelocity;

	[SerializeField]
	private bool m_UseTracker;

	public Vector3 GetPosition()
	{
		return base.transform.position;
	}

	public Vector3 GetScreenPosition()
	{
		Vector3 result = Pools.SceneCamera.WorldToScreenPoint(GetPosition());
		result.z = 0f;
		return result;
	}

	public override void Start()
	{
		base.Start();
		OpenDrag();
		m_constraint = base.gameObject.GetComponent<Constraint>();
		m_constraint.enabled = false;
		m_constraint.adjustment = GetRadius();
		Reset();
		if (m_UseTracker)
		{
			Debugger2.Tracker tracker = Debugger2.DBERP.GetComponentWithType(Debugger2.DebuggerType.Tracker, base.gameObject) as Debugger2.Tracker;
			tracker.SetTransform(base.gameObject.transform);
		}
	}

	public void setTarget(PoolBall ball, Vector3 targetPos)
	{
		if ((bool)ball)
		{
			m_targetBall = ball;
			m_targetPos = targetPos;
			ref Vector3 targetPos2 = ref m_targetPos;
			Vector3 position = base.transform.position;
			targetPos2.y = position.y;
		}
	}

	public override void OnCollisionEnter(Collision col)
	{
		if (GameManager.Rules.State == GlobalState.DRAG_WHITEBALL)
		{
			return;
		}
		if (col.gameObject.name.Contains("Rail"))
		{
			AudioManager.BallhitRail(m_Rigidbody.velocity);
			GameManager.Rules.CueBallHitRail();
			GameManager.Rules.BallHitRail(this);
			GameStatistics.MarkCueballHitRail(1);
		}
		if (!col.transform.CompareTag("Ball"))
		{
			return;
		}
		if ((m_state & (State.POTTED | State.HIDE)) == State.NONE || (col.gameObject.GetComponent<PoolBall>().BallState & (State.POTTED | State.HIDE)) == State.NONE)
		{
			AudioManager.BallhitBall(m_Rigidbody.velocity);
		}
		GameStatistics.MarkCueballHitBall(1);
		PoolBall component = col.gameObject.GetComponent<PoolBall>();
		GameManager.Rules.WhiteBallHitBall(component);
		if ((bool)component && component == m_targetBall)
		{
			m_targetBall.PointAtTarget(m_targetPos);
			m_targetBall = null;
		}
		for (int i = 0; i < Pools.BallsArray.Length; i++)
		{
			Pools.BallsArray[i].OpenDrag();
		}
		if (GameManager.GType == GameType.Mission)
		{
			for (int j = 0; j < Pools.CustomBallsArray.Length; j++)
			{
				Pools.CustomBallsArray[j].OpenDrag();
			}
		}
	}

	public override void Update()
	{
		base.Update();
		m_TestVelocity = m_Rigidbody.velocity;
	}

	public void OnDrawGizmos()
	{
		if (Application.isPlaying)
		{
			Gizmos.DrawRay(m_Rigidbody.position, m_TestVelocity);
		}
	}

	public override void Reset()
	{
		base.Reset();
		if (GameManager.GType == GameType.Mission)
		{
			Pools.PutBallToThePoint(this, ref m_PrevRoundPosition);
			return;
		}
		Vector3 p = Pools.CueBallOrigin.position;
		Pools.PutBallToThePoint(this, ref p);
	}

	public void fireBall(float powerScalar, Vector3 fireDir, Vector3 hitPoint)
	{
		AudioManager.FireBall(powerScalar);
		GameManager.Rules.OnBallFired();
		m_slowTime = 0f;
		Vector3 force = fireDir * powerScalar * ConstantData.MaxImpulse;
		m_Rigidbody.AddForceAtPosition(force, hitPoint, ForceMode.Impulse);
		m_state = State.ROLL;
		ConstantData.MaxImpulse = GameConfig.MAX_IMPULSE;
		ConstantData.GuidelineLength = 60;
	}

	public override void Potted(PocketIndexes pocketIndex)
	{
		if ((bool)m_Rigidbody)
		{
			m_state = State.POTTED;
			m_Rigidbody.velocity = Vector3.zero;
			m_Rigidbody.angularVelocity = Vector3.zero;
			CloseRenderer();
			base.enabled = false;
			RemovePhysicalMaterial();
			GameStatistics.MarkCueballPotted(1);
		}
	}
}
